package com.itheima.d03_net_chat.client;

import java.io.*;
import java.net.Socket;
import java.util.Objects;
import java.util.Scanner;

/**
 * 目标：创建客户端，发送数据给服务端
 */
public class Client {

    public static void main(String[] args) throws Exception {

        Scanner sc = new Scanner(System.in);
        try (
                //1.创建客户端Socket对象，与指定的服务器端建立连接（通信管道）
                Socket clientScoket = new Socket("192.168.15.35", 10086);
                //2.获取Socket的输出流对象
                OutputStream os = clientScoket.getOutputStream();
                PrintWriter pw = new PrintWriter(os);

                //获取Socket的输入流对象
                InputStream is = clientScoket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
        ) {

            //子线程：接收服务器返回的结果
            new Thread(new ReceiveThread(clientScoket)).start();

            //主线程：持续的发送数据给服务器
            while (true) {
                System.out.println("请输入要发送的数据：");
                String msg = sc.next();
                //3.写数据给服务器
                pw.println(msg);
                pw.flush();

                if (Objects.equals("886", msg)) {
                    System.out.println("客户端端口... ...");
                    break;
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
